package com.bwie.service.impl;

import com.bwie.mapper.InterrogationMapper;
import com.bwie.pojo.R;
import com.bwie.pojo.dto.InterrogationDto;
import com.bwie.pojo.po.InterrogationPo;
import com.bwie.service.InterrogationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class InterrogationServiceImpl implements InterrogationService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private InterrogationMapper interrogationMapper;

    /**
     * 列表
     * @param interrogationDto
     * @return
     */
    @Override
    public R getInterList(InterrogationDto interrogationDto) {
        // 从redis中获取列表
        List<InterrogationPo> inter = (List<InterrogationPo>) redisTemplate.opsForValue().get("inter");
        // 如果redis中没有列表，则从数据库中获取
        if(CollectionUtils.isEmpty(inter)){
            inter = interrogationMapper.getInterList(interrogationDto);
            // 将列表存入redis
            redisTemplate.opsForValue().set("inter", inter);
        }
        // 设置分页参数
        PageHelper.startPage(interrogationDto.getPageNum(),interrogationDto.getPageSize());
        // 从数据库中获取列表
        inter = interrogationMapper.getInterList(interrogationDto);
        // 将列表封装成PageInfo对象
        PageInfo<InterrogationPo> pageInfo = new PageInfo<>(inter);
        // 返回结果
        return R.ok().data(pageInfo);
    }

    /**
     * 单删
     * @param interrogationId
     * @return
     */
    @Override
    public R del(Integer interrogationId) {
        if(interrogationId == null){
            return R.err().msg("参数异常");
        }
        interrogationMapper.del(interrogationId);
        /**
         * 对应的中间表
         */
        interrogationMapper.toInterDel(interrogationId);
        return R.ok();
    }

    /**
     * 批删
     * @param interDel
     * @return
     */
    @Override
    public R delAll(List<Integer> interDel) {
        if(CollectionUtils.isEmpty(interDel)){
            return R.err().msg("参数异常");
        }
        /**
         * 删除问诊表
         */
        interrogationMapper.delAll(interDel);
        /**
         * 对应的中间表
         */
        interrogationMapper.toInterChangeDel(interDel);
        return R.ok();
    }
}
